import 'package:ad_common/third_ad.dart';
import 'package:flutter/foundation.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'google_ad_manager.dart';

/// FileName google_native_ad
/// @Author zhuqingfang
/// @Date 2022/9/30 5:24 下午
/// @Description google native ad
class GoogleNativeAd extends ThirdAd{
  GoogleNativeAd({required ThirdAdParams thirdAdParams})
      : super(thirdAdParams: thirdAdParams);
  NativeAd?  _nativeAd;
  @override
  void load() {
    if (!isSdkInit) {
      init();
    }
    _loadNativeAd();
  }

  @override
  void show() {
    debugPrint("show in flutter widget");
  }
  void _loadNativeAd(){
    final NativeAdListener nativeAdListener = NativeAdListener(
      // Called when an ad is successfully received.
      onAdLoaded: (Ad ad) {
        debugPrint('google native ad loaded.');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adLoaded,
            'google native ad placementID:${thirdAdParams.placementId} loaded');
      },
      // Called when an ad request failed.
      onAdFailedToLoad: (Ad ad, LoadAdError error) {
        // Dispose the ad here to free resources.
        debugPrint('google native ad failed to load: $error');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adLoadFail,
            'google native ad placementID:${thirdAdParams.placementId} load fail');
        ad.dispose();
      },
      // Called when an ad opens an overlay that covers the screen.
      onAdOpened: (Ad ad) {
        debugPrint('google native ad opened.');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adOnShow,
            'google native ad placementID:${thirdAdParams.placementId} onShow');
      },
      // Called when an ad removes an overlay that covers the screen.
      onAdClosed: (Ad ad) {
        debugPrint('google native ad closed.');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adOnDismissed,
            'google native ad placementID:${thirdAdParams.placementId} onDismiss');
      },
      // Called when an impression occurs on the ad.
      onAdImpression: (Ad ad) => debugPrint('Ad impression.'),
      // Called when a click is recorded for a NativeAd.
      onAdClicked:(Ad ad){
        debugPrint('google native ad on Click');
        thirdAdParams.thirdAdEventCallback(AdCallbackEvent.adOnClicked,
            'google native ad placementID:${thirdAdParams.placementId} onClick');
      } ,
    );
    _nativeAd = NativeAd(
      adUnitId: thirdAdParams.placementId,
      factoryId: 'adFactoryExample',
      request: const AdRequest(),
      listener: nativeAdListener,
    );
    _nativeAd?.load();
  }
}